/**
 *  *2010-6-29 下午01:18:26  add 
 */
package com.base.myproject.client.HR.Salary;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.CommandSyncContainer;
import com.base.myproject.client.tools.CommandSyncReturnObject;
import com.base.myproject.client.tools.CommandSyncsql;
import com.base.myproject.client.tools.Debug;
import com.base.myproject.client.tools.GreetingService;
import com.base.myproject.client.tools.GreetingServiceAsync;
import com.base.myproject.client.ui.BaseGrid;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.CheckColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * 
 * @author Administrator 2010-6-29 javazgw@gmail.com
 */
public class SelectGrid extends LayoutContainer{
	BaseStore<BusiObject> mstore = null; // 主表
	// 必须有 code，name ，set,upcode

	String tablename ;
	String sql ;
	String upcode ;
	String sheetcode ;
	
	public SelectGrid(BaseStore<BusiObject> mstore ,String tablename,String sql,String upcode,String sheetcode) {
		this.mstore = mstore;
		this.tablename = tablename;
		this.sql = sql;
		this.upcode =upcode;
		this.sheetcode =sheetcode;
		init();
	}

	private void init() {
		initWidget();
	
	}

	private void initWidget() {
		add(createGrid());
	}
	BaseStore<BusiObject> store = null;
	BaseStore<BusiObject> getStore(){
	if(store==null) 
	{
		createStore();
	}
	return store;}
	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("code");
		type.getField("code").setType(String.class);
		type.addField("name");
		type.getField("name").setType(String.class);
		type.addField("isset");
		type.getField("isset").setType(Boolean.class);
		type.addField("upcode");
		type.getField("upcode").setType(String.class);
		type.addField("sheetcode");
		type.getField("sheetcode").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename(tablename);
		info.setKeyname(new String[] { upcode });
		info
				.setSql(sql);
		info.setForcenolimit("true");
		info.setNeedAllRow("false");
		info.setParent(mstore);// 关联主表
		 info.setLinkparentcolumns(new String[] { upcode }); // 关联主表字段
		 info.setLinkparent_chile_columns(new String[] { "upcode" });//
		// 关联主表字段
		store = new BaseStore<BusiObject>(type, info) {
			
			public java.util.Vector<String> _trySave() {
				java.util.Vector<String> v = new java.util.Vector<String>();
				if(upcode==null || mstore.getValue(upcode)==null)
				{
					Info.display("错误",  "系统内部错误，未找到主表upcode");
					return null;
					
				}
				String delsql = "delete " +tablename +" where sheetcode='"+sheetcode+"' AND upcode = '"+mstore.getValue(upcode)+"'";
				v.add(delsql);
				String sql = "";
				for (BusiObject bo : store.getModels()) {
				if (bo.get("isset").equals(Boolean.TRUE)) {
					
					sql = "insert into "+tablename+" (code,name,isset,upcode,sheetcode) values('"
							+ bo.get("code") + "','" + bo.get("name")  + "',1,'"+mstore.getValue(upcode)+"','"+sheetcode+"') ";
					Debug.printf(sql, 4);
					v.add(sql);
				} 
				}
				commitChanges();
				return v;
			}
			public boolean trySave() {
				
				if(upcode==null || mstore.getValue(upcode)==null)
				{
				
					MessageBox.alert("错误"	, "系统内部错误，未找到主表upcode"	, null	);
					return false;
					
				}
				
				CommandSyncsql csql = new CommandSyncsql();
				String delsql = "delete " +tablename +" where sheetcode='"+sheetcode+"' AND upcode = '"+mstore.getValue(upcode)+"'";
				Debug.printf(delsql, 4);
				 csql.getV().add(delsql);
				for (BusiObject bo : store.getModels()) {
					
					String sql = "";
					if (bo.get("isset").equals(Boolean.TRUE)) {
						
						sql = "insert into "+tablename+" (code,name,isset,upcode,sheetcode) values('"
								+ bo.get("code") + "','" + bo.get("name")  + "',1,'"+mstore.getValue(upcode)+"','"+sheetcode+"') ";
						Debug.printf(sql, 4);
						csql.getV().add(sql);
					} 
					//v.add(sql);			
					
					GWT.log("保存"+sql);
					
				}
					//--------
					
				if(csql.getV().size()<=0)
					return false;
				
					 CommandSyncContainer list = new CommandSyncContainer();
					 list.add(csql);
				
					 GreetingServiceAsync greetingService = GWT
						.create(GreetingService.class);
					 greetingService
						.SendCommandSync(
								list,
								new AsyncCallback<CommandSyncReturnObject>() {
									@Override
									public void onFailure(
											Throwable caught) {
										MessageBox.alert("错误",
												"网络连接失败", null);
										
									}
									@Override
									public void onSuccess(
											CommandSyncReturnObject result) {
										if (result.isB()) {
											Info.display("", "添加成功");
										}
										else{
											MessageBox.alert("错误",
													"错误，错误代码"+result.getMessage(), null);
											
										}
									}
								});
					//--------
			
				return true;
			}
		};
		return store;
	}

	public BaseGrid grid=null;
	BaseGrid createGrid(){
	CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
	List<ColumnConfig> columns = new ArrayList<ColumnConfig>();

	ColumnConfig C2 = new ColumnConfig("name", "名称", 80);
	columns.add(C2);

	 CheckColumnConfig checkColumn = new CheckColumnConfig("isset", "选择", 55);  
	final CheckBox cb= new CheckBox();
	    CellEditor checkBoxEditor = new CellEditor(cb);  
	    checkColumn.setEditor(checkBoxEditor);  
	columns.add(checkColumn);

	ColumnModel cm = new ColumnModel(columns);
	grid = new BaseGrid(getStore(), cm,false	,false);
	grid.addPlugin(checkColumn); 
	grid.setBorders(false);


	return grid ;}

	public String getTablename() {
		return tablename;
	}

	public void setTablename(String tablename) {
		this.tablename = tablename;
	}

	public String getSql() {
		return sql;
	}

	public void setSql(String sql) {
		this.sql = sql;
	}

	public String getUpcode() {
		return upcode;
	}

	public void setUpcode(String upcode) {
		this.upcode = upcode;
	}

	public void setStore(BaseStore<BusiObject> store) {
		this.store = store;
	}
	
	
}
